<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
    $data=file_get_contents('php://input');
	$jsondata=json_decode($data,true);	
	$action=$jsondata["action"];
//	file_put_contents("test.log", "1..".$controlerNum."2..".$gatenum, FILE_APPEND);
//    if($action!="downLoad") return;
    if($action!="downLoad")
	{
		echo "fail";
		return;
	}
	$WebADDR = $jsondata["WebADDR"];
    $WebPORT = $jsondata["WebPORT"];
	$cmdresultpath = $jsondata["cmdresultpath"];
	$realtimepath = $jsondata["realtimepath"];
	$DetectDevicepath = $jsondata["DetectDevicepath"];
	$Camera0ADDR = $jsondata["Camera0ADDR"];
    $Camera0PORT = $jsondata["Camera0PORT"];
	$Camera1ADDR = $jsondata["Camera1ADDR"];
    $Camera1PORT = $jsondata["Camera1PORT"];
	$Camera2ADDR = $jsondata["Camera2ADDR"];
    $Camera2PORT = $jsondata["Camera2PORT"];
	$Camera3ADDR = $jsondata["Camera3ADDR"];
    $Camera3PORT = $jsondata["Camera3PORT"];
	$reQusttime = $jsondata["reQusttime"];
	if(!$WebADDR||!$WebPORT||!$cmdresultpath||!$realtimepath||!$DetectDevicepath||!$Camera0ADDR||!$Camera0PORT||!$Camera1ADDR||!$Camera1PORT||!$Camera2ADDR||!$Camera2PORT||!$Camera3ADDR||!$Camera3PORT||!$reQusttime)
	{	    
//		echo "<script language=\"JavaScript\">alert('输入为空或太长。');</script><br/>";
//		file_put_contents("test.log", "\n null \n", FILE_APPEND);
		echo "fail";
		return;	
	}
	$myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        $string = "Can't connect database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
	    die('Could not connect: '.mysql_error());
		echo "fail";
		return;
    }
    $db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {        
	    die("Can't use database dwaccessDB : ".mysql_error());
        $string = "Can't use database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		mysql_close($myconn);
		echo "fail";
		return;
	}
	$strSql="SELECT COUNT(*) FROM serversetting;";
	$result11=mysql_query($strSql,$myconn);
	$dbResult11 = mysql_fetch_array($result11);
	mysql_free_result($result);
//	file_put_contents("test.log", "\n strSql".$strSql, FILE_APPEND);
	if($dbResult11[0])
	{
//	    $strSql="UPDATE serversetting SET serverIP= '$WebADDR',serverPORT='$WebPORT',cmdresultpath='$cmdresultpath',realtimepath='$realtimepath',DetectDevicepath='$DetectDevicepath',Camera0ADDR='$Camera0ADDR',Camera0PORT='$Camera0PORT',Camera1ADDR='$Camera1ADDR',Camera1PORT='$Camera1PORT',Camera2ADDR='$Camera2ADDR',Camera2PORT='$Camera2PORT',Camera3ADDR='$Camera3ADDR',Camera3PORT='$Camera3PORT',reQusttime='$reQusttime',sVersion='$sVersion';";
	    $strSql="UPDATE serversetting SET serverIP= '$WebADDR',serverPORT='$WebPORT',cmdresultpath='$cmdresultpath',realtimepath='$realtimepath',DetectDevicepath='$DetectDevicepath',Camera0ADDR='$Camera0ADDR',Camera0PORT='$Camera0PORT',Camera1ADDR='$Camera1ADDR',Camera1PORT='$Camera1PORT',Camera2ADDR='$Camera2ADDR',Camera2PORT='$Camera2PORT',Camera3ADDR='$Camera3ADDR',Camera3PORT='$Camera3PORT',reQusttime='$reQusttime';";
//		$result=mysql_query($strSql,$myconn);
        	    
//	    $string = "\n strSql:".$strSql."\n";
//	    file_put_contents("test.log",  "\n strSql:".$strSql."\n", FILE_APPEND);
	}else
	{
//        $strSql="INSERT INTO serversetting(serverIP,serverPORT,cmdresultpath,realtimepath,DetectDevicepath,Camera0ADDR,Camera0PORT,Camera1ADDR,Camera1PORT,Camera2ADDR,Camera2PORT,Camera3ADDR,Camera3PORT,reQusttime,sVersion) VALUES('$WebADDR','$WebPORT',$cmdresultpath,$realtimepath,$DetectDevicepath,'$Camera0ADDR','$Camera0PORT','$Camera1ADDR','$Camera1PORT','$Camera2ADDR','$Camera2PORT','$Camera3ADDR','$Camera3PORT','$reQusttime','$sVersion');";
	    $strSql="INSERT INTO serversetting(serverIP,serverPORT,cmdresultpath,realtimepath,DetectDevicepath,Camera0ADDR,Camera0PORT,Camera1ADDR,Camera1PORT,Camera2ADDR,Camera2PORT,Camera3ADDR,Camera3PORT,reQusttime) VALUES('$WebADDR','$WebPORT','$cmdresultpath','$realtimepath','$DetectDevicepath','$Camera0ADDR','$Camera0PORT','$Camera1ADDR','$Camera1PORT','$Camera2ADDR','$Camera2PORT','$Camera3ADDR','$Camera3PORT','$reQusttime');";
//		$result=mysql_query($strSql,$myconn);
		
//		$string = "strSql:".$strSql."\n";
//	    file_put_contents("test.log",  "\n strSql:".$strSql."\n", FILE_APPEND);	
	}
	$result11=mysql_query($strSql,$myconn);
	mysql_free_result($result11);
	$exec="SELECT serverIP,serverPORT,cmdresultpath,realtimepath,DetectDevicepath,Camera0ADDR,Camera0PORT,Camera1ADDR,Camera1PORT,Camera2ADDR,Camera2PORT,Camera3ADDR,Camera3PORT,reQusttime FROM serversetting;";
	$result=mysql_query($exec); 
	if((mysql_affected_rows()==0) or (mysql_affected_rows==-1)) 
	{ 
		mysql_free_result($result);
		mysql_close($myconn);
		$string = "Can't exec database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		echo "fail";
		exit; 
	}
    $item = mysql_fetch_row($result);
	mysql_free_result($result);	
//	$cmdstr="{\"trans_id\":\"".$transid."\",\"cmd_code\":\"DW_SET_WEB_SERVERINFO\",\"url\":\"$item[0]\",\"port\":\"$item[1]\",\"cmdresultpath\":\"$item[2]\",\"realtimepath\":\"$item[3]\",\"DetectDevicepath\":\"$item[4]\",\"Camera0ADDR\":\"$item[5]\",\"Camera0PORT\":\"$item[6]\",\"Camera1ADDR\":\"$item[7]\",\"Camera1PORT\":\"$item[8]\",\"request_time\":\"$item[9]\"}";					
//	@file_put_contents("test.log", "\n cmdstr =".$cmdstr."\n", FILE_APPEND);
	$exec="SELECT DISTINCT controlerNum FROM gatesetting WHERE controlerNum !='ALL';";
    $result=mysql_query($exec);    
    while($item1 = mysql_fetch_row($result))
	{
        $transid=uuid();
		$cmdstr="{\"trans_id\":\"".$transid."\",\"cmd_code\":\"DW_SET_WEB_SERVERINFO\",\"url\":\"$item[0]\",\"port\":\"$item[1]\",\"cmdresultpath\":\"$item[2]\",\"realtimepath\":\"$item[3]\",\"DetectDevicepath\":\"$item[4]\",\"Camera0ADDR\":\"$item[5]\",\"Camera0PORT\":\"$item[6]\",\"Camera1ADDR\":\"$item[7]\",\"Camera1PORT\":\"$item[8]\",\"Camera2ADDR\":\"$item[9]\",\"Camera2PORT\":\"$item[10]\",\"Camera3ADDR\":\"$item[11]\",\"Camera3PORT\":\"$item[12]\",\"request_time\":\"$item[13]\"}";					
		$strSql="INSERT INTO cmdform(controlerNum,transid,cmdstr) VALUES('".$item1[0]."','".$transid."','".$cmdstr."');";
        $result1=mysql_query($strSql,$myconn);
		mysql_free_result($result1);
//		@file_put_contents("test.log", "\n strSql =".$strSql."\n", FILE_APPEND);
	}
	mysql_free_result($result);
	mysql_close($myconn);
    echo "success";	
//	echo "<script language=\"JavaScript\">alert('提交成功。');</script><br/>";
//	@file_put_contents("test.log", "\n strSql =".$strSql."\n", FILE_APPEND);
//    echo "<script language=\"JavaScript\">self.setTimeout(window.location.href='/door/serversetting.php',500);</script><br/>";
	
    function uuid() 
	{
        if (function_exists('com_create_guid')) 
		{
            return  md5(com_create_guid());
        }else 
		{
            mt_srand((double)microtime()*10000);                   //optional for php 4.2.0 and up.
            $charid = md5(uniqid(rand(),true)); 
            return $charid;
        }
    }
	exit;
?>

	